$(document).ready(function(e) {
    inputFocusBlur();
	closeLayer();/*关闭弹出层*/
	limite_input($(".limite-input"),200);//文本框输入限制字数
});

function limite_input($obj,num){//文本框输入限制字数
	var limitNum = num; 
	var pattern = '还可以输入' + limitNum + '字'; 
	$obj.siblings('.show-limite').html(pattern); 
	$obj.keyup( function(){
		var remain = $(this).val().length; 
		if(remain > num){ 
			pattern = '字数超过'+num +'个限制！';  
		}else{ 
			var result = limitNum - remain;  
			pattern = '还可以输入' + result + '字'; 
		} 
		$obj.siblings('.show-limite').html(pattern); 
	} ); 
}

function checkIsMobile(val){
	var isMobile = checkPhone($.trim(val));
	if(!isMobile){
		$("#err-msg").show().html("请输入正确的手机号码！");
	}else{
		$("#err-msg").hide();
	}
}
function checkPhone(mb){
	var mobile = mb;
	if(mobile != '' && mobile.length == 11){
		var reg0 = /^13\d{5,9}$/; 
		var reg1 = /^15\d{5,9}$/; 
		var reg2 = /^18\d{5,9}$/; 
		var reg3 = /^14\d{5,9}$/;  
		
		var my = false; 
		if (reg0.test(mobile)) my=true; 
		if (reg1.test(mobile)) my=true; 
		if (reg2.test(mobile)) my=true; 
		if (reg3.test(mobile)) my=true; 
		return my; 
	}
}



/*close layer*/
function closeLayer(){
	var $layerbox = $("#layer-box"),
		$btnClose = $layerbox.find(".btn-close");
	$btnClose.bind('click',function(){
		$layerbox.hide();
	});
}
/*show layer*/
function showLayer($elem,title){
	var $layerbox = $("#layer-box"),
	$layerbg = $layerbox.find(".layer-bg"),
	$title = $layerbox.find(".layer-title .title"),
	$layerwrap = $layerbox.find(".layer-wrap"),
	docWidth = $(document).width(),
	docHg = $(document).height();
	$layerbg.css({"height":docHg,"width":docWidth});
	$title.html(title);
	$layerbox.show();
	$elem.show().siblings().hide();
}
function progress(){
	var percent = $('.progress_bar').attr('title');
	$(".progress_bar").animate({width:percent},1000);
}
/*倒计时*/
function countdown ($elem,time){
	var obj = $elem;
	this.doTiming = function (){
		var st;
		if (time<=0){
			clearTimeout(st);
		} else {
			time--;
			st=setTimeout (arguments.callee, 1000);
			obj.html(time +"秒后可重发短信");
			if(time == 0){
				obj.text("重新获取短信验证码").removeClass("disabled");
			}
		}
	}
}
function get_phone_code(elem,time){
	var $elem = $(elem);
	if(!$elem.hasClass("disabled")){
		$elem.addClass("disabled");
		var cd = new countdown($elem,time);
		cd.doTiming();
	}
}

/*input focus and blur*/
function inputFocusBlur($elem){
	$(".input-event").each(function(index, element) {
		var $input = $(this);
       	var $ph = $input.siblings(".placeholder");
		$input.val('');
		$input.bind('focus',function(){
			if($.trim($input.val()) == ''){
				$ph.hide();
			}
			show_error($input,true);
			$input.addClass("input-text-focus");
		}).bind('blur',function(){
			if($.trim($input.val()) == ''){
				$ph.show();
				$input.val("");
			}
			$input.removeClass("input-text-focus");
		});
		$ph.bind('click',function(){
			$ph.hide();
			$input.focus();
		});
    });
	
}
function show_error($elem,isTrue,txt){
	if(!isTrue){
		$elem.parentsUntil(".control-group").parent().addClass("error");
		$elem.siblings(".err-msg").removeClass("vi-hide").addClass("vi-show").find(".txt").html(txt);
		$elem.siblings(".err-msg").show();
	}else{
		$elem.parentsUntil(".control-group").parent().removeClass("error");
		$elem.siblings(".err-msg").removeClass("vi-show").addClass("vi-hide");
		$elem.siblings(".err-msg").hide();
	}
}

function fjifen(s, n) {
	n = n > 0 && n <= 20 ? n : 2;
	s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
	var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
	t = "";
	for (i = 0; i < l.length; i++) {
		t += l[i]
				+ ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
	}
	// return t.split("").reverse().join("") + "." + r;
	return t.split("").reverse().join("");
}